# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: polynomial_fit_util.py
  @time: 2022/7/15 10:33
  @desc:
"""

from scipy.signal import savgol_filter
import numpy as np
from matplotlib import pyplot as plt




def normalized(p0, p_list):
    if p0 and p_list:
        value = float(p_list[0]) - float(p0)
        cal_p_list = list(map(lambda x: float(x) - float(value), p_list[1:]))
        cal_p_list.insert(0, p0)
        return cal_p_list


def polynomial_fit(v_list,p_v_list, num=2):
    y_data = list(map(lambda x: np.arccos(2 * pow(10, float(x) / 10) - 1), p_v_list))
    z1 = np.polyfit(v_list, y_data, num)
    p1 = np.poly1d(z1)
    a, b, c = z1
    v_pi = np.pi / abs(4 * float(a) - float(b))
    return v_pi,z1

def plot_curve(curve_list):
    plt.figure(figsize=(20, 20), dpi=100)
    fig, ax = plt.subplots()
    ax.set_title(f"VPI curve")
    for i in range(len(curve_list)):
        ax.plot(curve_list[i][0], curve_list[i][1], label=str(i))
        ax.set_xlabel("Voltage")
        ax.set_ylabel("Power")
        ax.grid(True)
        ax.legend()
    ax.invert_yaxis()
    plt.show()


p0 = -3.374
p01 = -3.426

# # #
# #


v2_p2 = [-22.52296067,
-22.36108122,
-22.20159531,
-22.03724945,
-21.89067968,
-21.79573546,
-21.67298083,
-21.52566634,
-21.37516552,
-21.24041071,
-21.09781732,
-20.99268342,
-20.90894805,
-20.78688494,
-20.67670674,
-20.60034117
]
v_p2 = [-22.63756286,
-22.61866041,
-22.60051451,
-22.55605464,
-22.53724628,
-22.49747636,
-22.49418451,
-22.48859215,
-22.45787599,
-22.60861608,
-22.61325094,
-22.62413173,
-22.64768474,
-22.58621936,
-22.62362141,
-22.60785273

]
v1_p1 = [-22.47437711,
-22.63652715,
-22.7698803,
-22.94940185,
-23.08869743,
-23.27160749,
-23.42426977,
-23.61856226,
-23.78717402,
-23.95262336,
-24.17083316,
-24.34810067,
-24.50819164,
-24.77930112,
-24.97165256,
-25.15812366
]
v_p1 = [-22.63570446,
-22.67753007,
-22.64675373,
-22.65291616,
-22.62693935,
-22.62146124,
-22.61134297,
-22.64081996,
-22.66655138,
-22.63504706,
-22.6449854,
-22.63115862,
-22.6040217,
-22.60343615,
-22.60379918,
-22.61108738

]
# v_p12 = normalized(p0,v_p2)
# v_p11 = normalized(p0,v_p1)
# v_list = [0.0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6000000000000001, 0.7000000000000001, 0.8, 0.9, 1.0, 1.1, 1.2000000000000002, 1.3, 1.4000000000000001, 1.5]
# # v_p11 = [-3.361, -3.5274210444444414, -3.648442949999996, -3.809950877777773, -4.013935255555555, -4.181996594444435, -4.328516161111104, -4.516832094444439, -4.702574355555555, -4.904583338888884, -5.102799461111111, -5.345597966666659, -5.571912083333327, -5.808625705555549, -6.049253611111105, -6.351584905555555]
# # v_p12 = [-3.361, -3.217305233333338, -3.0818116388888974, -2.9511856055555583, -2.8399197833333396, -2.6961953388888915, -2.5844944444444486, -2.475003933333337, -2.382437555555562, -2.241780133333336, -2.12438876111111, -2.066334777777783, -1.9607467166666694, -1.8862134666666712, -1.7769904166666706, -1.6763620944444462]
# # # p_v_list = normalized(p0, p1)
# # # print(p_v_list)
# # v_p110 = [i+0.5 for i in v_p11]
# print(polynomial_fit(v_list,v_p12))
# print(polynomial_fit(v_list,v_p11))
